package recursion.leetcode.editor.cn;


import recursion.leetcode.editor.cn.common.ListNode;

import java.util.LinkedList;


public class PalindromeLinkedListLcci {
    public static void main(String[] args) {
        Solution solution = new PalindromeLinkedListLcci().new Solution();
        ListNode a = new ListNode(1);
        ListNode b = new ListNode(2);
        ListNode c = new ListNode(3);
        a.next = b;
        b.next = c;
        System.out.println(solution.isPalindrome(a));
        System.out.println(solution.isPalindrome(a.next));
        System.out.println(solution.isPalindrome(c.next));
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode(int x) { val = x; }
     * }
     */
    class Solution {

        ListNode temp;

        public boolean isPalindrome(ListNode head) {
            temp = head;
            return check(head);
        }

        private boolean check(ListNode head) {
            if (head == null) {
                return true;
            }
            boolean res = check(head.next) && (temp.val == head.val);
            temp = temp.next;
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}